<?php

require_once "../common/login.php";

$string_list = "//ul[@class='land_holder']/li";
$string_quality = "./div[2]";
$string_storage = ".//div[@class='inside']/a/span";
$string_raw = "./a";

$username = "usher@21cn.com";
$userpass = "flyingfish";

$raw_types = array('iron' => 'raw1', 'oil' => 'raw2', 'aluminum' => 'raw3', 'saltpeter' => 'raw4', 'rubber' => 'raw5',
										'grain' => 'raw1', 'fruits' => 'raw2', 'fish' => 'raw3', 'cattle' => 'raw4', 'deer' => 'raw5');

function login($name, $passwd)
{
	global $link;

	if ( $link->login($name, $passwd) == TRUE ) {
		echo "Login succeed.\r\n";
	} else {
		die("Login failed.\r\n");
	}
}

function Parse($id,$name)
{
	global $string_list, $string_quality, $string_storage, $string_raw;
	global $raw_types;
	global $link, $fhandle;

	$company = array('raw1' => 0, 'raw2' => 0, 'raw3' => 0, 'raw4' => 0, 'raw5' => 0,
							'storage1' => 0, 'storage2' => 0, 'storage3' => 0,
							'one' => 0, 'two' => 0, 'three' => 0, 'four' => 0, 'five' => 0,
							'id' => $id, 'name' => $name);
	
	$filename = "http://economy.erepublik.com/en/land/overview/$id";
	
	do {
		$file = $link->getPage($filename);
	} while ( $file === FALSE );
	
	$doc = new DOMDocument();
	$doc->loadHTML($file);
	$xpath = new DOMXpath($doc);

	$list = $xpath->query($string_list);
	
	if ($list->length > 0) { // skip the first building
		for ($i = 1; $i < $list->length; ++$i) {
			$land = $list->item($i);
			if ($land->hasAttribute('class')) {
				$elements = $xpath->query($string_storage, $land);
				if ($elements->length > 0) {
					// it's a storge
					preg_match("/(\d+)/", $elements->item(0)->textContent, $match);
					$capacity = intval($match[1]);
					switch ($capacity) {
						case 1000:
							++$company['storage1'];
							break;
						case 9000:
							++$company['storage2'];
							break;
						case 20000:
							++$company['storage3'];
							break;
						default:
							die("Unknown Storage Type: $capacity\n");
					}
				} else {
					// it's a raw
					$elements = $xpath->query($string_raw, $land);
					$type = trim( strrchr( $elements->item(0)->getAttribute('class'), " " ) );
					if (array_key_exists($type, $raw_types)) {
						++$company[$raw_types[$type]];
					} else {
						die( "Unknown Raw Type: $type\n" );
					}
				}
			} else {
				// it's a manufactory
				$elements = $xpath->query($string_quality, $land);
				$quality = trim( strrchr( $elements->item(0)->getAttribute('class'), " " ) );
				++$company[$quality];
			}
		}
	}
	
	$content = serialize($company) . "\r\n";
	fwrite($fhandle, $content);
}

//main
if ($argc != 2) {
	die("Usage: $argv[0] idx_file\n");
}

$tmp_file = 'tmp.dat';
$data_file = 'data.dat';

$ids = file($argv[1], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = array();
if (file_exists($tmp_file)) {
	$lines = file($tmp_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}

$link = new loginObj();
$start_line = count($lines);
$end_line = count($ids);

login($username, $userpass);

libxml_use_internal_errors(true);

$fhandle = fopen($tmp_file, 'a');

for ($i = $start_line; $i < $end_line; $i++) {
	$a = explode(',', trim($ids[$i]));
	Parse($a[0],$a[1]);
	$total = $i+1;
	echo "------Parsed $total citizen------\n";
}

fclose($fhandle);

unlink($data_file);
copy($tmp_file, $data_file);
unlink($tmp_file);

?>